/*
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2024/1/12 14:26
@File     : main.go
@Project  : gohub
@Desc     :
*/
package main

import (
	"fmt"
	"github.com/spf13/cobra"
	"gohub/app/cmd"
	make2 "gohub/app/cmd/make"
	"gohub/bootstap"
	baseConfig "gohub/config"
	"gohub/pkg/config"
	"gohub/pkg/console"
	"os"
)

func init() {
	baseConfig.Initialize()
}

func main() {
	//应用主入口，默认调用cmd.CmdServe命令
	var rootCmd = &cobra.Command{
		Use:   "Gohub",
		Short: "A simple forum project",
		Long:  `Default will run "serve" command, you can use "-h" flag to see all subcommands.`,
		//rootCmd所有的子命令都会执行以下代码
		PersistentPreRun: func(command *cobra.Command, args []string) {
			//配置初始化，依赖命令行 --env参数
			config.InitConfig(cmd.Env)
			//初始化Logger
			bootstap.SetUpLogger()
			//初始化DB
			bootstap.SetupDB()
			//初始化Redis
			bootstap.SetupRedis()
			//初始化缓存
			bootstap.SetupCache()
		},
	}
	//注册子命令
	rootCmd.AddCommand(
		cmd.CmdServe,
		cmd.CmdKey,
		cmd.CmdPlay,
		make2.CmdMake,
		cmd.CmdMigrate,
		cmd.CmdDBSeed,
		cmd.CmdCache,
	)

	//配置默认运行的web服务
	cmd.RegisterDefaultCmd(rootCmd, cmd.CmdServe)
	//注册全局参数，如 --env
	cmd.RegisterGlobalFlags(rootCmd)

	//执行主命令
	if err := rootCmd.Execute(); err != nil {
		console.Exit(fmt.Sprintf("Failed to run app with %v: %s", os.Args, err.Error()))
	}
}
